<template>
  <div>
    <common-head title="购物车" />
    <div class="wrap">
      <div class="items">
        <van-swipe-cell v-for="(item, index) in items" :key="item.id">
          <div class="item">
            <van-checkbox @click="switchCheck(index)" v-model="item.selected" />
            <van-card
              :num="item.number"
              :price="item.price"
              :title="item.name"
              :thumb="item.pic"
            >
              <template #footer>
                <van-stepper @change="changeNum(index)" v-model="item.number" />
              </template>
            </van-card>
          </div>
          <template #right>
            <van-button
              square
              text="删除"
              type="danger"
              @click="delItem(item.key)"
              class="delete-button"
            />
          </template>
        </van-swipe-cell>
      </div>
      <van-submit-bar :price="total * 100" button-text="提交订单" @submit="onSubmit">
        <van-checkbox v-model="allChecked">全选</van-checkbox>
      </van-submit-bar>
    </div>
    <common-tab />
    <van-empty
      v-show="!$store.state.cart.items.length"
      class="custom-image"
      image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
      description="靓仔这里啥都没有了"
    />
  </div>
</template>

<script>
import CommonTab from 'components/CommonTab'
import CommonHead from 'components/CommonHead'
import { fetchCartItems, setByNumber, switchCheck, delCartItem } from 'api'
import { Dialog } from 'vant'
import { getToken } from 'utils'

export default {
  name: 'cart',
  data () {
    return {
      items: [],
      total: 0
      // empty: false
    }
  },
  created () {
    this.fetchCartItems()
    // if (this.$store.state.cart.items.length === 0) {
    //   this.empty = true
    // }
  },
  watch: {
    items () {
      this.$store.commit('cart/set_items', this.items)
    }
  },
  methods: {
    delItem (key) {
      // 给确认框删除
      Dialog.confirm({
        title: '删除商品',
        message: '您确认删除吗'
      })
        .then(() => {
          // on confirm
          delCartItem({
            key,
            token: getToken()
          }).then(res => {
            if (res.data.code === 0) {
              this.items = res.data.data.items
              this.total = res.data.data.price
            }
            if (res.data.code === 700) {
              // 全部删除
              this.items = []
              this.total = 0
              this.empty = true
            }
            if (this.$store.state.cart.items.length === 0) {
              this.empty = true
            }
          })
        })
    },
    onSubmit () {
      this.$router.push('/confirmorder')
    },
    switchCheck (index) {
      const { key, selected } = this.items[index]
      switchCheck({
        key,
        selected,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
        }
      })
    },
    changeNum (index) {
      console.log(this.items[index])
      const { key, number } = this.items[index]
      setByNumber({
        key,
        number,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
        }
      })
    },
    fetchCartItems () {
      fetchCartItems({
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
        }
      })
    }
  },
  computed: {
    allChecked: {
      get () {
        return this.items.length === 0 ? false : this.items.every(item => item.selected)
      },
      set (val) {
        // 改变全选修改单选状态
        // 改变单选状态
        // 找到所有商品的key
        const keys = []
        this.items.forEach((item) => {
          keys.push(item.key)
        })
        switchCheck({
          key: keys.join(','),
          selected: val,
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.items = res.data.data.items
            this.total = res.data.data.price
          }
        })
      }
    }
  },
  components: {
    CommonTab,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  padding-top: 50px;
  padding-bottom: 100px;
  .delete-button {
    height: 100%;
  }
  .item {
    display: flex;
    background-color: #fafafa;
    .van-checkbox {
      margin: 0 5px;
    }
    .van-card {
      flex: 1;
    }
  }
  .van-submit-bar {
    bottom: 50px;
  }
}
</style>
